const isEncoded = (uri) => {
  uri = uri || '';
  try {
    return uri !== decodeURIComponent(uri);
  } catch (error) { // JM：未编码的URI中包含%，就会发生uri malformed的问题
    console.warn(`uri malformed error[Ignored] ${error}`);
    return false;
  }
}

const encodeURIWrapper = (url) => {
  if (isEncoded(url)) {
    return url;
  } else {
    url = encodeURIComponent(url.trim());
    return url.replaceAll("(", "%28").replaceAll(")", "%29").replaceAll("_", "%5F");
    // // url = encodeURI(url.trim());
    // url = url.trim().replaceAll("%", "%25").replaceAll('"', "22%").replaceAll("?", "%3F").replaceAll("[", "%5B")
    //     .replaceAll("\\", "%5C").replaceAll("]", "%5D").replaceAll("^", "%5E").replaceAll("　", "%E3%80%80");
    // return url.trim().replaceAll("#", "%23").replaceAll(":", "%3A").replaceAll("(", "%28").replaceAll(")", "%29").replaceAll("@", "%40").replaceAll("\n", "%0A")
    //     .replaceAll("<", "%3C").replaceAll(">", "%3E").replaceAll(" ", "%20").replaceAll("/", "%2F").replaceAll("&", "%26");
  }
}

const decodeURIWrapper = (url) => {
  url = decodeURIComponent(url);
  return url.replaceAll("%28", "(").replaceAll("%29", ")").replaceAll("%5F", "_");
  // url = decodeURI(url.trim());
  // return url.replaceAll("%E3%80%80", "　").replaceAll("%23", "#").replaceAll("%3A", ":").replaceAll("%28", "(").replaceAll("%29", ")").replaceAll("%0A", "\n")
  //     .replaceAll("%3C", "<").replaceAll("%3E", ">").replaceAll("%20", " ").replaceAll("%2F", "/").replaceAll("%40", "@").replaceAll("%3F", "?").replaceAll("%25", "%").replaceAll("%26", "&");
  // if (isEncoded(url)) {
  //
  // } else {
  //     return url;
  // }
}

/**
 * 取出中括号内的内容
 * @param text
 * @param left
 * @param right
 * @returns {string}
 */
const getHighlightStr = (text, left="-,", right=",-") => {
  let result = text
  if (!text)
    return result
  // let regex = /-,(.+?),-/g;
  let regex = new RegExp(`${left}(.+?)${right}`, 'g');
  let options = text.match(regex)
  if (options) {
    let option = options[0]
    if (option) {
      result = option.substring(left.length, option.length - right.length)
    }
  }
  return result
}

const getQuery = (element) => {
  if (element.id) {
    return `#${escapeCSSString(element.id)}`;
  }
  if (element.localName === 'html') {
    return 'html';
  }

  const parent = element.parentNode;

  const parentSelector = getQuery(parent);
  // The element is a text node
  if (!element.localName) {
    // Find the index of the text node:
    const index = Array.prototype.indexOf.call(parent.childNodes, element);
    return `${parentSelector}>textNode:nth-of-type(${index})`;
  } else {
    const index = Array.from(parent.childNodes).filter((child) => child.localName === element.localName).indexOf(element) + 1;
    return `${parentSelector}>${element.localName}:nth-of-type(${index})`;
  }
}

const escapeCSSString = (cssString) => {
  return cssString.replace(/(:)/ug, "\\$1");
}

export {
  encodeURIWrapper,
  decodeURIWrapper,
  getHighlightStr,
  getQuery
}
